package coreModel;

import java.text.DecimalFormat;

import hitExceptions.InvalidDataException;



/**
 * This class contains a unit type and size that is associated with a product
 * @author The Fantastic 4
 *
 */
@SuppressWarnings("serial")
public class Supply extends SerializableImplementer{


	private float supplySize;
	private Unit supplyUnit;
	
	
	/**
	 * Constructor, build an empty supply
	 */
	public Supply()
	{
		
	}
	
	/**
	 * Constructor that builds a full supply object, throws an exception if the size is wrong
	 * @param supplySize
	 * @param unit
	 * @throws InvalidDataException
	 */
	public Supply(float supplySize, Unit unit) throws InvalidDataException
	{
		this.setSupplySize(supplySize);
		this.setSupplyUnit(unit);
	}
	/**
	 * @return the supplySize
	 */
	public float getSupplySize()
	{
		return supplySize;
	}
	
	/**
	 * @param supplySize the supplySize to set
	 * @throws InvalidDataException 
	 */
	public void setSupplySize(float supplySize) throws InvalidDataException
	{
		if(supplySize<0)
		{
			throw new InvalidDataException("Size must be positive");
		}
		this.supplySize = supplySize;
	}
	
	/**
	 * @return the supplyUnit
	 */
	public Unit getSupplyUnit()
	{
		return supplyUnit;
	}
	
	/**
	 * @param supplyUnit the supplyUnit to set
	 */
	public void setSupplyUnit(Unit supplyUnit)
	{
		this.supplyUnit = supplyUnit;
	}
	
	/**
	 * if size is less than 0 it throws an error
	 * @throws InvalidDataException
	 */
	public void validateSupplySize() throws InvalidDataException 
	{
		if(supplySize<0)
		{
			throw new InvalidDataException("Size must be positive");
		}
		
		//need to also check the UNIT part of the supply
		
		
	}
	
	/**
	 * To string to when converting to XML
	 */
	@Override
	public String toString()
	{
		String tmp="";
		DecimalFormat formatForFloats=new DecimalFormat("#.##");
		tmp+=formatForFloats.format(supplySize);
		tmp+=" ";
		if (supplyUnit == Unit.FLUID_OUNCES)
			tmp+="fluid ounces";
		else
			tmp+=supplyUnit.toString().toLowerCase();
		return tmp;
	}
		

}
